package com.elmcode.server.config;

import com.elmcode.server.interceptor.LoginInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.FormContentFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
@Slf4j
public class MyWebConfig extends WebMvcConfigurationSupport {

    @Value("${Knife4.adminPackageName}")
    private String adminPackageName;

    @Value("${Knife4.appPackageName}")
    private String appPackageName;

    @Autowired
    private LoginInterceptor loginInterceptor;

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        //这个要实现：拦截以admin开头的请求，但是放行登录接口的请求
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/admin/**")
                .addPathPatterns("/app/**")
                .excludePathPatterns("/admin/user/login")
                .excludePathPatterns("/app/customer/login");
    }

    @Bean
    public FormContentFilter createFormContentFilter(){
        return new FormContentFilter();
    }


    @Bean
    public Docket createDocketAdmin() {
        ApiInfo apiInfo = new ApiInfoBuilder()
            .title("饿了么项目接口文档app")
            .version("1.0")
            .description("饿了么项目接口文档app")
            .build();

        Docket docket = new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo)
            .groupName("管理端")
            .select()
            .apis(RequestHandlerSelectors.basePackage(adminPackageName))
            .paths(PathSelectors.any())
            .build();
        return docket;
    }


    @Bean
    public Docket createDocketApp() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("饿了么项目接口文档app")
                .version("1.0")
                .description("饿了么项目接口文档app")
                .build();

        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .groupName("移动端")
                .select()
                .apis(RequestHandlerSelectors.basePackage(appPackageName))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Override
    protected void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*") // 允许所有源访问，可以指定具体的源
                .allowedMethods("GET", "POST", "PUT", "DELETE") // 允许的请求方法
                .allowedHeaders("*") // 允许所有请求头
                .allowCredentials(true) // 是否允许发送Cookie
                .maxAge(3600); // 预检请求的有效期，单位为秒
    }
}

